<template>
  <div class="container">
    <el-card class="box-card">
      <!-- 搜索目录 测试-->
      <div v-if="this.$route.query.id" slot="header" class="clearfix">
        <span>学科管理  </span>
        <i class="el-icon-arrow-right"></i>
        <span> {{ this.$route.query.name }} </span>
        <i class="el-icon-arrow-right"></i>
        <span>  目录管理</span>
      </div>
      <el-row>
        <el-col :span="20">
          <div>
            <el-form
              ref="query"
              v-model="query"
              prop="value"
              label-width="80px"
            >
              <el-form-item label="目录名称" class="filter">
                <el-input v-model="query.directoryName"></el-input>
              </el-form-item>
              <el-form-item label="状态" class="filter">
                <el-select placeholder="请选择" v-model="query.state">
                  <el-option
                    v-for="item in status"
                    :key="item.value"
                    :label="item.label"
                    :value="item.value"
                  ></el-option>
                </el-select>
              </el-form-item>
              <el-button @click="onEmpty">清除</el-button>
              <el-button type="primary" @click="onSearch(formData)"
                >搜索</el-button
              >
            </el-form>
          </div>
        </el-col>
        <!-- 添加目录 -->
        <el-col :span="4">
          <div>
            <el-link
              v-if="this.$route.query.id"
              :underline="false"
              class="link el-icon-back"
              @click="$router.push('list')"
              >返回学科</el-link
            >
            <el-button
              class="contents"
              type="primary"
              icon="el-icon-edit"
              @click="directory"
              >新增目录</el-button
            >
          </div>
        </el-col>
        <!-- 数据数量 -->
        <el-alert
          :title="`数据一共 ${total} 条`"
          type="info"
          show-icon
          :closable="false"
        >
        </el-alert>
        <!-- 目录数据 -->
        <el-table
          ref="singleTable"
          :data="tableData"
          highlight-current-row
          style="width: 100%"
        >
          <!-- @current-change="handleCurrentChange" -->
          <el-table-column type="index" label="序号" width="80">
          </el-table-column>
          <el-table-column
            prop="subjectName"
            label="所属学科"
            width="220"
          ></el-table-column>
          <el-table-column
            prop="directoryName"
            label="目录名称"
            width="220"
          ></el-table-column>
          <el-table-column
            prop="username"
            label="创建者"
            width="200"
          ></el-table-column>
          <el-table-column label="创建时间" width="240">
            <template slot-scope="scope">
              <span>{{ scope.row.addDate | formatTime }}</span>
            </template>
          </el-table-column>
          <el-table-column
            prop="totals"
            label="面试题数量"
            width="200"
          ></el-table-column>
          <el-table-column align="center" label="状态" width="220">
            <template slot-scope="scope">
              <span v-if="scope.row.state == 1">已启用</span>
              <span v-else>已禁用</span>
            </template>
          </el-table-column>
          <el-table-column label="操作">
            <template slot-scope="scope">
              <el-button type="text" size="small" @click="editState(scope.row)">
                <span v-if="scope.row.state == 1">启用</span>
                <span v-else>禁用</span>
              </el-button>
              <el-button
                type="text"
                size="small"
                :disabled="scope.row.state == 0"
                @click="editDirectory(scope.row.id)"
                >修改</el-button
              >
              <el-button
                type="text"
                size="small"
                :disabled="scope.row.state == 0"
                @click="deleteDirectory(scope.row.id)"
                >删除</el-button
              >
            </template>
          </el-table-column>
        </el-table>
      </el-row>
      <!-- dialog -->
      <el-dialog
        :title="isEdit ? '编辑目录' : '添加目录'"
        width="25%"
        :visible.sync="showDialog"
      >
        <directorys-add
          v-if="showDialog"
          @clone="showDialog = false"
          @addSub="addSubject"
          :is-edit="isEdit"
          :id="id"
          :query="query"
        />
      </el-dialog>
      <!-- 分页 -->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="query.page"
        :page-sizes="[10, 20, 30, 40]"
        background
        :page-size="query.size"
        layout="prev, pager, next, sizes, jumper"
        :total="total"
      >
      </el-pagination>
    </el-card>
  </div>
</template>

<script>
// 状态
import { status } from '@/api/hmmm/constants'
// 调用接口
import { list, remove, changeState } from '@/api/hmmm/directorys.js'
// 导入组件
import directorysAdd from '../components/directorys-add.vue'
// 引入 dayjs
import dayjs from 'dayjs'
export default {
  name: 'directorys',
  components: {
    directorysAdd
  },
  filters: {
    formatTime (time) {
      if (!time) return
      return dayjs(time).format('YYYY-MM-DD hh:mm:ss')
    }
  },
  data () {
    return {
      status: status, // 状态
      id: 0, // 文章 id
      isEdit: false, // 显示编辑获取新增
      tableData: [], // 目录列表
      total: 0, // 数据总数
      showDialog: false, // 弹框隐藏和显示
      formData: {
        name: '', // 目录名称
        state: '' // 搜索状态
      },
      query: {
        page: 1, // 页码
        pagesize: 10, // 每页请求数据
        directoryName: null, // 目录名称
        state: null, // 状态
        subjectID: this.$route.query.id || null // 学科ID
      }
    }
  },
  created () {
    // 调用获取目录列表数据的方法
    this.directoryList()
  },
  watch: {
    $route: 'getPath'
  },

  methods: {
    getPath () {
      this.query.subjectID = null
      this.directoryList(this.query)
    },
    // 搜索信息
    onSearch () {
      this.query.page = 1
      this.directoryList(this.query)
    },

    // 点击清空数据空
    onEmpty () {
      this.query.directoryName = null
      this.query.state = null
    },

    // 目录状态
    async editState (row) {
      try {
        await changeState({
          id: row.id,
          state: row.state === 1 ? 0 : 1
        })
        this.$message.success('操作成功')
        row.state = row.state === 1 ? 0 : 1
      } catch (e) {
        this.$message.error('请求错误!')
      }
    },
    // 删除目录
    async deleteDirectory (id) {
      // 显示删除询问对话框
      const delRes = await this.$confirm(
        '此操作将永久删除该角色, 是否继续?',
        '提示',
        {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }
      ).catch((err) => err)

      // 用户点击了取消，给用户进行提示
      if (delRes === 'cancel') return this.$message('您取消了删除')

      // 调用删除的 API
      const res = await remove(id)

      // 判断当前数据的长度是否等于 1
      if (this.tableData.length === 1) {
        this.query.page--
        // 如果是第一页的最后一条
        if (this.query.page <= 0) {
          this.query.page = 1
        }
      }

      // 删除成功后的提示
      if (res.status === 200) {
        this.$message.success('删除成功!')
      }
      // 重新获取数据
      this.directoryList()
    },

    // 重新渲染，关闭弹框
    addSubject () {
      this.directoryList()
      this.showDialog = false
    },

    // 编辑
    editDirectory (id) {
      // console.log(id)
      this.id = id
      this.isEdit = true
      this.showDialog = true
    },

    // 添加弹框
    directory () {
      // 显示添加
      this.isEdit = false
      this.showDialog = true
    },

    // 获取目录列表数据
    async directoryList (val) {
      const res = await list(this.query)
      // console.log(3)
      this.tableData = res.data.items
      this.total = res.data.counts
    },

    // 每页显示的条数发生变化时触发
    handleSizeChange (newSize) {
      // console.log(newSize)
      this.query.pagesize = newSize
      this.directoryList()
    },

    // 页码发生变化时触发
    handleCurrentChange (newPage) {
      // console.log(newPage)
      this.query.page = newPage
      this.directoryList()
    }
  }
}
</script>

<style  scoped>
.clearfix {
  height: 10px;
  font-size: 14px;
  line-height: 1;
  color: #606266;
}
.link {
  padding: 10px 0 0 50px;
  display: inline-block;
  color: #66b1ff;
}
.el-pagination {
  float: right;
  margin: 10px;
}

.contents {
  float: right;
  background: #5daf34;
  border-color: #5daf34;
  color: #fff;
}
button.el-button {
  font-size: 12px;
  border-radius: 3px;
  margin: 0 7px 0;
}
.container {
  margin: 11px;
}

.box-card {
  width: 100%;
}
.filter {
  display: inline-block;
}
</style>
